﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using YiLong.Framework.Entity;
using YiLong.Framework.Config;
using YiLong.Framework.DataAccess.Config;

namespace YiLong.Framework.DataAccess.SqlBuilder
{
    public class PagingSqlConditionBuilder : SqlConditionBuilder
    {
        int _pageIndex;
        int _pageSize;

        public PagingSqlConditionBuilder(DataCommand dbCommand, EasyPagingQuery pagingInfo, string defaultOrderBy)
            : base(dbCommand, defaultOrderBy,pagingInfo.sort,pagingInfo.order)
        {
            _pageIndex = pagingInfo.page;
            _pageSize = pagingInfo.rows;
            //如果分页索引从1开始，自动减1
            if (ConfigManager.DataAccessConfig.PageIndexStart == 1)
            {
                _pageIndex = _pageIndex - 1;
            }
            //默认分页索引从0开始
            int startRowID = _pageIndex * _pageSize + 1;
            int endRowID = (_pageIndex + 1) * _pageSize;

            //dbCommand.AddParameter("@StartRowID", DbType.Int32, 4, ParameterDirection.Input, startRowID);
            //dbCommand.AddParameter("@EndRowID", DbType.Int32, 4, ParameterDirection.Input, endRowID);
            //dbCommand.AddOutParameter("@TotalCount", DbType.Int32, 4);
        }

        public int GetTotalCount()
        {
            object totalCount = 0;//_dbCommand.GetParameterValue("@TotalCount");

            return totalCount == null || totalCount == DBNull.Value ? 0 : Convert.ToInt32(totalCount);
        }
    }
}
